package com.kk.service.impl;


import com.kk.Entry.Dept;
import com.kk.Entry.DeptLog;
import com.kk.Entry.Employee;
import com.kk.Mapper.DeptMapper;
import com.kk.Mapper.EmpMapper;
import com.kk.aop.log;
import com.kk.service.DeptLogService;
import com.kk.service.DeptService;
import com.kk.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptMapper deptMapper;
    @Autowired
    DeptLogService deptLogService;


    @Override
    public void inquire(String name) {
        List<Dept> inquire = deptMapper.inquire(name);
        System.out.println(inquire);
    }

    @Override
    public void inquireLimit() {
        List<Dept> inquire = deptMapper.inquireLimit();
        System.out.println(inquire);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delDep(Integer id) {

        int delDep = deptMapper.delDep(id);
        int deleteEmp = deptMapper.deleteEmpsByDepId(id);
        int i = deptLogService.insertDeptLog(new DeptLog(null, LocalDateTime.now(), "删除" + delDep + "和员工：" + deleteEmp));
        return delDep;
    }


    @Override@log
    public List<Dept> SelectDept() {
        return deptMapper.selectDept();
    }

    @Override
    public int addDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        return deptMapper.addDept(dept);
    }

    @Override
    public int update(Dept dept) {

        dept.setUpdateTime(LocalDateTime.now());

        return deptMapper.update(dept);
    }


    public Dept selectById(Integer id) {

        return deptMapper.selectById(id);
    }
}
